﻿using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
    public float m_speed = 1;
    public Transform m_rocket;
    public float m_rocketRate = 0;
    protected Transform m_transform;
    //生命
    public float m_life = 10;


    // 声音
    public AudioClip m_shootClip;

    // 声音源
    protected AudioSource m_audio;

    // 爆炸特效
    public Transform m_explosionFX;


	// Use this for initialization
	void Start () {
        m_transform = this.transform;

        m_audio = this.GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update () {
	

        //纵向移动距离
        float movev = 0;

        //水平移动距离
        float moveh = 0;

        //按上键
        if (Input.GetKey(KeyCode.UpArrow))
        { movev -= m_speed * Time.deltaTime;  }

        //按下键
        if (Input.GetKey(KeyCode.DownArrow))
        { movev += m_speed * Time.deltaTime;  }

        //按左键
        if (Input.GetKey(KeyCode.LeftArrow))
        { moveh += m_speed * Time.deltaTime;  }

        //按右键
        if (Input.GetKey(KeyCode.RightArrow))
        { moveh -= m_speed * Time.deltaTime;  }

        //移动
        this.m_transform.Translate(new Vector3(moveh, 0, movev));

        //发射子弹频率
        m_rocketRate -= Time.deltaTime;
        if (m_rocketRate <= 0)
        {
            m_rocketRate = 0.1f;

            //按空格键或鼠标左键发射子弹

            if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
            {   //散射
                Instantiate(m_rocket, m_transform.position, m_transform.rotation);
                if( GameManager.Instance.m_score>=300)
                Instantiate(m_rocket, m_transform.position, Quaternion.AngleAxis(155, Vector3.up));
                if (GameManager.Instance.m_score >= 600)
                    Instantiate(m_rocket, m_transform.position, Quaternion.AngleAxis(205, Vector3.up));
                if (GameManager.Instance.m_score >= 1200)
                {
                    Instantiate(m_rocket, m_transform.position, Quaternion.AngleAxis(190, Vector3.up));
                    Instantiate(m_rocket, m_transform.position, Quaternion.AngleAxis(170, Vector3.up));
                }


                // 播放设计声音
                m_audio.PlayOneShot(m_shootClip);
            
            }
        }
	}

    void OnTriggerEnter(Collider other)
    {
        if (other.tag.CompareTo("PlayerRocket") != 0)
        { m_life -= 1;
        if (m_life <= 0)
        {
            Instantiate(m_explosionFX,m_transform.position,Quaternion.identity);
            Destroy(this.gameObject); 
        
        }
        }
    }
}
